package com.atguigu.customer;

import com.atguigu.constant.MqConst;
import com.atguigu.entity.SkuInfo;
import com.atguigu.service.SearchService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;

/**
 * @Description
 * @Author: TT
 * @Date: 2022/8/8 17:45
 */
@Component
public class EsConsumer {
    @Autowired
    private SearchService searchService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    //接受上架消息，默认采用自动签收
    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(value = MqConst.ON_SALE_QUEUE,durable = "false"),
            exchange = @Exchange(value =MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key = {MqConst.ON_SALE_ROUTING_KEY}
    ))
    public void onSale(Long skuId, Channel channel, Message message) throws IOException {
        if(!StringUtils.isEmpty(skuId)){
            searchService.onSale(skuId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        System.out.println("我已经发送要上架的信息了");
    }
    //接受下架消息，默认采用自动签收
    @RabbitListener(bindings = @QueueBinding(
            value =@Queue(value = MqConst.OFF_SALE_QUEUE,durable = "false"),
            exchange = @Exchange(value =MqConst.ON_OFF_SALE_EXCHANGE,durable = "false"),
            key = {MqConst.OFF_SALE_ROUTING_KEY}
    ))
    public void offSale(Long skuId, Channel channel, Message message) throws IOException {
        if(!StringUtils.isEmpty(skuId)){
            searchService.offSale(skuId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        System.out.println("我已经发送要下架的信息了");
    }
}
